package com.sxx.mall.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxx.commons.utils.PageUtils;
import com.sxx.commons.utils.Query;
import com.sxx.mall.product.dao.AttrGroupDao;
import com.sxx.mall.product.entity.AttrEntity;
import com.sxx.mall.product.entity.AttrGroupEntity;
import com.sxx.mall.product.service.AttrGroupService;
import com.sxx.mall.product.service.AttrService;
import com.sxx.mall.product.vo.AttrGroupWithAtrrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询列表数据
     *    根据列表编号来查询
     * @param params
     * @param catelogId 如何catelogId为0 就不根据catelogId来查询
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // 获取检索的关键字
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            // 拼接查询的条件
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        if(catelogId == 0){
            // 不根据catelogId来查询
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),wrapper
            );
            return new PageUtils(page);
        }
        // 根据类别编号来查询属性信息
        wrapper.eq("catelog_id",catelogId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),wrapper
        );
        return new PageUtils(page);
    }

    //根据catelogId获取属性组及其属性信息
    @Override
    public List<AttrGroupWithAtrrsVo> getAttrgroupWithAttrsByCatelogId(Long catelogId) {
        //根据catelogId获取所有属性组信息
        List<AttrGroupEntity> attrGroups = this.list(new QueryWrapper<AttrGroupEntity>()
                        .eq("catelog_id", catelogId));
        List<AttrGroupWithAtrrsVo> attrs = attrGroups.stream().map(group -> {
            AttrGroupWithAtrrsVo attrGroupWithAtrrsVo = new AttrGroupWithAtrrsVo();
            BeanUtils.copyProperties(group, attrGroupWithAtrrsVo);
            //根据属性组获取所有属性信息
            List<AttrEntity> attrEntities = attrService.getRelationAttr(group.getAttrGroupId());
            attrGroupWithAtrrsVo.setAttrs(attrEntities);
            return attrGroupWithAtrrsVo;
        }).collect(Collectors.toList());

        return attrs;
    }

}